package com.hb.trade.server.project.evaluation.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hb.core.pojo.parent.PageResult;
import com.hb.core.pojo.project.evaluation.dto.EvaluationCommitteeDTO;
import com.hb.core.pojo.project.evaluation.dto.EvaluationCommitteeMembersDTO;
import com.hb.core.pojo.project.evaluation.entity.*;
import com.hb.core.pojo.project.proj.entity.BidInviterVO;
import com.hb.core.pojo.project.proj.enums.BidWayEnum;
import com.hb.core.pojo.project.section.dto.BidSectionDTO;
import com.hb.core.pojo.project.section.entity.BidSection;
import com.hb.trade.server.project.evaluation.service.*;
import com.hb.trade.server.project.section.service.raw.BidSectionService;
import org.fzzn.core.model.enums.LogicEnum;
import org.fzzn.core.model.enums.MsgEnum;
import org.fzzn.core.model.msg.Message;
import org.fzzn.core.util.Enums;
import org.fzzn.core.util.MsgUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 评标委员会信息 前端控制器
 * </p>
 *
 * @author lixiang
 * @since 2021-12-19 14:10:12
 */
@RestController
public class EvaluationCommitteeController {

    @Autowired
    private EvaluationCommitteeService evaluationCommitteeService;

    @Autowired
    private EvaluationCommitteeBidService evaluationCommitteeBidService;

    @Autowired
    private EvaluationCommitteeMembersService evaluationCommitteeMembersService;

    @Autowired
    private EvaluationCommitteeAvoidOrgService evaluationCommitteeAvoidOrgService;

    @Autowired
    private BidSectionService bidSectionService;

    @Autowired
    private AntianaphylaxisService antianaphylaxisService;

    /**
     * 根据项目Id查询未被评委会选择的标段信息
     *
     * @param evaluationCommitteeDTO
     * @return
     */
    @PostMapping("/evaluationCommittee/listNotSelectSection")
    public Message<PageResult> listNotSelectSection(@RequestBody EvaluationCommitteeDTO evaluationCommitteeDTO) {
        if (null == evaluationCommitteeDTO.getProjectId()) {
            return MsgUtil.fail(MsgEnum.INCORRECT_PARAM.msg());
        }
        Page<BidSection> bidSectionList = evaluationCommitteeService.listNotSelectSection(evaluationCommitteeDTO);
        return MsgUtil.success(PageResult.get(bidSectionList));
    }

    /**
     * 评委会信息保存
     *
     * @param evaluationCommitteeDTO
     * @return
     */
    @PostMapping("/evaluationCommittee/save")
    @Transactional(rollbackFor = Exception.class)
    public Message saveEvaluationCommittee(@RequestBody EvaluationCommitteeDTO evaluationCommitteeDTO) {
        if (CollUtil.isEmpty(evaluationCommitteeDTO.getSectionList())) {
            return MsgUtil.fail("关联标段(包)不可为空");
        }
        if (null == evaluationCommitteeDTO.getId()) {
            //保存
            evaluationCommitteeDTO.setStatus(1);
            evaluationCommitteeService.saveEvaluationCommittee(evaluationCommitteeDTO);
        } else if (null != evaluationCommitteeDTO.getId() && evaluationCommitteeDTO.getStatus() == 1) {
            evaluationCommitteeService.updateEvaluationCommittee(evaluationCommitteeDTO);
        } else {
            return MsgUtil.fail(MsgEnum.FAILURE.msg());
        }
        if (evaluationCommitteeDTO.getSmsFlag() == LogicEnum.YES.getCode()) {
            // TODO: 2021-9-23  缺少发送短信业务
            System.out.println("-");
        }
        return MsgUtil.success(evaluationCommitteeDTO);
    }

    /**
     * 评委会发布
     *
     * @param evaluationCommitteeDTO
     * @return
     */
    @PostMapping("/evaluationCommittee/publishEvaluationCommittee")
    public Message publishEvaluationCommittee(@RequestBody EvaluationCommitteeDTO evaluationCommitteeDTO) {
        if (null == evaluationCommitteeDTO.getId()) {
            return MsgUtil.fail(MsgEnum.INCORRECT_PARAM.msg());
        }
        evaluationCommitteeDTO.setStatus(2);
        Boolean result = evaluationCommitteeService.updateById(evaluationCommitteeDTO);
        return result ? MsgUtil.msg(MsgEnum.SUCCESS) : MsgUtil.fail(MsgEnum.FAILURE.msg());
    }

    /**
     * 评委会分页查询
     *
     * @param evaluationCommitteeDTO
     * @return
     */
    @PostMapping("/evaluationCommittee/pageEvaluationCommittee")
    public Message pageEvaluationCommittee(@RequestBody EvaluationCommitteeDTO evaluationCommitteeDTO) {
        if (null == evaluationCommitteeDTO.getProjectId()) {
            return MsgUtil.fail(MsgEnum.INCORRECT_PARAM.msg());
        }
        Page<EvaluationCommittee> evaluationCommitteeList = evaluationCommitteeService.pageEvaluationCommittee(evaluationCommitteeDTO);
        PageResult<EvaluationCommittee> pageEvaluationCommitteeList = PageResult.get(evaluationCommitteeList);
        return MsgUtil.success(pageEvaluationCommitteeList);
    }

    /**
     * 根据主键Id或者标段Id查询评委会详情
     *
     * @param evaluationCommitteeDTO
     * @return
     */
    @PostMapping("/evaluationCommittee/detail")
    public Message evaluationCommitteeDetail(@RequestBody EvaluationCommitteeDTO evaluationCommitteeDTO) {
        EvaluationCommittee evaluationCommittee = null;
        if (null != evaluationCommitteeDTO.getId()) {
            evaluationCommittee = evaluationCommitteeService.getById(evaluationCommitteeDTO.getId());
        } else if (null != evaluationCommitteeDTO.getSectionId()) {
            evaluationCommittee = evaluationCommitteeService.findBySectionIdsAndEvaluationType(evaluationCommitteeDTO.getSectionId(), evaluationCommitteeDTO.getEvaluationType());
        } else {
            return MsgUtil.fail(MsgEnum.INCORRECT_PARAM.msg());
        }
        if (null == evaluationCommittee) {
            return MsgUtil.fail("还未组建评委会");
        }
        BeanUtil.copyProperties(evaluationCommittee, evaluationCommitteeDTO);
        //标段集合
        List<EvaluationCommitteeBid> evaluationCommitteeBidList = evaluationCommitteeBidService.list(
                new LambdaQueryWrapper<EvaluationCommitteeBid>().eq(EvaluationCommitteeBid::getEvaluationCommitteeId, evaluationCommittee.getId()));
        List<Long> sectionIds = evaluationCommitteeBidList.stream().map(EvaluationCommitteeBid::getSectionId).collect(Collectors.toList());
        List<BidSection> bidSectionList = bidSectionService.listByIds(sectionIds);
        evaluationCommitteeDTO.setSectionList(bidSectionList);
        //查询随机专家库详情
        EvaluationCommitteeMembers evaluationCommitteeMembers = new EvaluationCommitteeMembers();
        evaluationCommitteeMembers.setEvaluationCommitteeId(evaluationCommittee.getId());
        evaluationCommitteeMembers.setMemberType(3);
        List<EvaluationCommitteeMembers> randomExpertInfos = evaluationCommitteeMembersService.selectInfoByEvaluationCommitteeIdAndMemberType(evaluationCommitteeMembers);
        //对专家的身份证和手机号进行脱敏处理
        List<EvaluationCommitteeMembersDTO> antiRandomExpertInfos = antianaphylaxisService.antiExpertList(randomExpertInfos);
        evaluationCommitteeDTO.setRandomExpertList(antiRandomExpertInfos);

        //查询指定专家库详情
        List<EvaluationCommitteeMembers> expertInfos = evaluationCommitteeMembersService.list(
                new LambdaQueryWrapper<EvaluationCommitteeMembers>().eq(EvaluationCommitteeMembers::getEvaluationCommitteeId, evaluationCommittee.getId()).eq(EvaluationCommitteeMembers::getMemberType, 1));
        //对专家的身份证和手机号进行脱敏处理
        List<EvaluationCommitteeMembersDTO> antiExpertInfos = antianaphylaxisService.antiExpertList(expertInfos);
        evaluationCommitteeDTO.setExpertList(antiExpertInfos);

        //查询招标人代表详情
        List<EvaluationCommitteeMembers> bidInviterVOList = evaluationCommitteeMembersService.list(
                new LambdaQueryWrapper<EvaluationCommitteeMembers>().eq(EvaluationCommitteeMembers::getEvaluationCommitteeId, evaluationCommittee.getId()).eq(EvaluationCommitteeMembers::getMemberType, 2));
        evaluationCommitteeDTO.setBidInviterList(bidInviterVOList);
        //处理回避企业信息
        List<EvaluationCommitteeAvoidOrg> avoidOrgList = evaluationCommitteeAvoidOrgService.list(
                new LambdaQueryWrapper<EvaluationCommitteeAvoidOrg>().eq(EvaluationCommitteeAvoidOrg::getEvaluationCommitteeId, evaluationCommitteeDTO.getId()));
        evaluationCommitteeDTO.setAvoidOrgList(avoidOrgList);

        return MsgUtil.success(evaluationCommitteeDTO);
    }


    /**
     * 查询评委会详情（导出用）
     *
     * @author tian
     * @date 2022-6-18 17:01
     * @param evaluationCommitteeDTO
     * @return org.fzzn.core.model.msg.Message
     */
    @PostMapping("/evaluationCommittee/detailByExport")
    public Message<EvaluationCommitteeDTO> detailByExport(@RequestBody EvaluationCommitteeDTO evaluationCommitteeDTO) {
        EvaluationCommittee evaluationCommittee = evaluationCommitteeService.getById(evaluationCommitteeDTO.getId());
        if(null == evaluationCommittee){
            return MsgUtil.fail("未查询到评委会信息");
        }
        BeanUtil.copyProperties(evaluationCommittee, evaluationCommitteeDTO);

        // 获取标段信息
        List<EvaluationCommitteeBid> evaluationCommitteeBidList = evaluationCommitteeBidService.list(
                new LambdaQueryWrapper<EvaluationCommitteeBid>().eq(EvaluationCommitteeBid::getEvaluationCommitteeId, evaluationCommittee.getId()));
        if(CollUtil.isEmpty(evaluationCommitteeBidList)){
            return MsgUtil.fail("未查询到评委会标段信息");
        }
        // 获取项目信息
        Long sectionId = evaluationCommitteeBidList.get(0).getSectionId();
        BidSectionDTO bidSectionInfo = bidSectionService.findBidSectionInfoById(sectionId);

        evaluationCommitteeDTO.setProjectName(bidSectionInfo.getProjectName());
        evaluationCommitteeDTO.setProjectCode(bidSectionInfo.getProjectCode());
        BidWayEnum bidWayEnum = Enums.of(BidWayEnum.class, bidSectionInfo.getBidWay());
        String bidWayName = bidWayEnum.getShowName();
        if(bidWayEnum.getCode() == 1 && bidSectionInfo.getCommonType().equals(2)){
            bidWayName = "邀请";
        }
        evaluationCommitteeDTO.setBidWayName(bidWayName);
        evaluationCommitteeDTO.setOpenBidTime(bidSectionInfo.getOpenBidTime());
        evaluationCommitteeDTO.setAgencyOrganizeInfo(bidSectionInfo.getAgencyOrganizeInfo());
        evaluationCommitteeDTO.setBiddingOrganizeInfo(bidSectionInfo.getBiddingOrganizeInfo());

        //查询随机专家库详情
        EvaluationCommitteeMembers evaluationCommitteeMembers = new EvaluationCommitteeMembers();
        evaluationCommitteeMembers.setEvaluationCommitteeId(evaluationCommittee.getId());
        List<EvaluationCommitteeMembers> expertList = evaluationCommitteeMembersService.selectInfoByEvaluationCommitteeIdAndMemberType(evaluationCommitteeMembers);
        List<EvaluationCommitteeMembersDTO> antiExpertInfos = antianaphylaxisService.antiExpertList(expertList);
        evaluationCommitteeDTO.setExpertList(antiExpertInfos);

        return MsgUtil.success(evaluationCommitteeDTO);
    }

    /**
     * 获取招标人代表列表
     *
     * @param bidInviterVO
     * @return
     */
    @PostMapping("/evaluationCommittee/getBidInviter")
    public Message getBidInviter(@RequestBody BidInviterVO bidInviterVO) {
        if (null == bidInviterVO || null == bidInviterVO.getOrganizeId()) {
            return MsgUtil.fail("参数为空");
        }
        //查询符合条件的招标人代表用户
        Page<BidInviterVO> bidInviterList = evaluationCommitteeService.pageBidInviter(bidInviterVO);
        PageResult<BidInviterVO> pageBidInviterList = PageResult.get(bidInviterList);
        return MsgUtil.success(pageBidInviterList);
    }



    /**
     * 根据主键Id评委会详情（给专家发送短信使用）
     * @author tian
     * @date 2022-2-27 9:14
     * @param evaluationCommitteeDTO
     * @return org.fzzn.core.model.msg.Message
     */
    @PostMapping("/evaluationCommittee/detailByPushSms")
    public Message<EvaluationCommitteeDTO> evaluationCommitteeDetailByPushSms(@RequestBody EvaluationCommitteeDTO evaluationCommitteeDTO) {
        EvaluationCommittee evaluationCommittee = evaluationCommitteeService.getById(evaluationCommitteeDTO.getId());
        if (null == evaluationCommittee) {
            return MsgUtil.fail("还未组建评委会");
        }
        BeanUtil.copyProperties(evaluationCommittee, evaluationCommitteeDTO);
        // 评委会下的标段集合
        List<EvaluationCommitteeBid> evaluationCommitteeBidList = evaluationCommitteeBidService.list(
                new LambdaQueryWrapper<EvaluationCommitteeBid>().eq(EvaluationCommitteeBid::getEvaluationCommitteeId, evaluationCommittee.getId()));

        // 评委会肯定是一个项目下的，所以直接获取根据标段查询项目的代理机构信息
        Long sectionId = evaluationCommitteeBidList.get(0).getSectionId();

        BidSectionDTO section = bidSectionService.findBidAndProjectBySectionId(sectionId);
        evaluationCommitteeDTO.setAgencyOrganizeInfo(section.getAgencyOrganizeInfo());

        //查询专家库详情
        List<EvaluationCommitteeMembers> expertInfos = evaluationCommitteeMembersService.list(
                new LambdaQueryWrapper<EvaluationCommitteeMembers>().eq(EvaluationCommitteeMembers::getEvaluationCommitteeId, evaluationCommittee.getId()));

        List<EvaluationCommitteeMembersDTO> evaluationCommitteeMembers = new ArrayList<>();
        for (EvaluationCommitteeMembers expertInfo : expertInfos) {
            EvaluationCommitteeMembersDTO evaluationCommitteeMembersDTO = new EvaluationCommitteeMembersDTO();
            BeanUtils.copyProperties(expertInfo, evaluationCommitteeMembersDTO);
            evaluationCommitteeMembers.add(evaluationCommitteeMembersDTO);
        }

        evaluationCommitteeDTO.setExpertList(evaluationCommitteeMembers);

        return MsgUtil.success(evaluationCommitteeDTO);
    }
}
